
import axios from 'axios'



export default {
    //get请求
    get(url, param) {
        let params = {
            ...param
        };
        return new Promise((resolve, reject) => {
            axios({
                method: 'get',
                url: url,
                headers: {'Auth-User-Token': sessionStorage.getItem("token")||'' },
                params: params
            }).then(res => {
                resolve(res.data)
            }).catch(e => {
                reject(e)
            });
        });
    },
    //----------------------------------------------post请求
    post(url, param, config) {
        let params = {
            ...param
        };
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                headers: {
                    'Content-Type': 'application/json;charset=utf-8',
                    'Auth-User-Token': sessionStorage.getItem("token")||''
                },
                data: params,
                url: url
            }).then(res => {
                resolve(res.data)
            }).catch(e => {
                reject(e)
            });
        });
    },
    //formdata -----------------------------------post请求
    formPost(url, params) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url: url,
                data: params,
                transformRequest: [function (data) {
                    let ret = ''
                    for (let it in data) {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                    return ret;
                }],
                headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'Auth-User-Token': sessionStorage.getItem("token")||'' }
            }).then(res => {
                resolve(res.data);
            }).catch(e => {
                reject(e)
            });
        });
    },
    //文件上传----------------------------请求
    formDataPost(url, params) {
        return new Promise((resolve, reject) => {
            axios({
                method: 'post',
                url: url,
                data: params,
                // transformRequest: [function (data) {
                //     let ret = ''
                //     for (let it in data) {
                //       ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                //     }
                //     return ret;
                // }],
                headers: { 'Content-Type': 'multipart/form-data;charset=utf-8', 'Auth-User-Token': sessionStorage.getItem("token")||'' }
            }).then(res => {
                resolve(res.data);
            }).catch(e => {
                reject(e)
            });
        });
    },
    // baseURL: API_ROOT
}